package com.atguigu.crud.service;

import java.io.IOException;
import java.util.List;

import net.sf.json.util.JSONUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.JstlUtils;

import com.alibaba.fastjson.JSON;
import com.atguigu.crud.bean.Wxuser;
import com.atguigu.crud.bean.WxuserExample;
import com.atguigu.crud.bean.WxuserExample.Criteria;
import com.atguigu.crud.dao.WxuserMapper;
import com.atguigu.crud.utils.JsonUtil;
import com.atguigu.weixin.kit.WeixinKit;
import com.atguigu.weixin.model.WeixinFianlValue;
import com.atguigu.weixin.model.WeixinUser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service("wxuserService")
public class WxuserService {
	@Autowired
	private	WxuserMapper wxuserMapper;//微信用户业务接口
	
	//查询微信用户信息
	public WeixinUser queryByOpenid(String openid) {
		String url=WeixinKit.replaceAccToken(WeixinFianlValue.USER_QUERY);
		url=url.replace("OPENID", openid);
		String json= WeixinKit.sendGet(url);
		return JSON.parseObject(json, WeixinUser.class);
	}
	
	//调微信接口 用户授权功能用到的  通过code换取网页授权access_token
	public String queryOpenidBycode(String code) {
		try {
			String url=WeixinFianlValue.AUTH_GET_OID;
			url=url.replace("APPID", WeixinFianlValue.APPID)
			.replace("SECRET", WeixinFianlValue.APPSECRET)
			.replace("CODE", code);
			String json=WeixinKit.sendGet(url);
			ObjectMapper m=new ObjectMapper();
			String openid=m.readTree(json).get("openid").asText();
			return openid;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	//查询数据微信对象
	public Wxuser  loadByOpenid(String openid){
		Wxuser u=new Wxuser();
		WxuserExample ue=new WxuserExample();
		Criteria criteria=ue.createCriteria();
		criteria.andOpenidEqualTo(openid);
		List<Wxuser> ulist=wxuserMapper.selectByExample(ue);
		 if(ulist.size()>0){
			 u=ulist.get(0);
		 }else{
			 u=null;
		 }
		return u;
	}
	
	//更新
    public void update(Wxuser u){
    	wxuserMapper.updateByPrimaryKeySelective(u);
    }
    
    //删除
    public void delete(int id){
    	wxuserMapper.deleteByPrimaryKey(id);
    }
    
    //添加
    public void add(Wxuser user){
    	Wxuser u =this.loadByWxusername(user.getUsername());
    	if(u!=null) throw new RuntimeException("用户名已经存在");
    	wxuserMapper.insert(user);
    }
	
    //查询对象
    public Wxuser loadByWxusername(String username) {
    	Wxuser u=new Wxuser();
		WxuserExample ue=new WxuserExample();
		Criteria criteria=ue.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<Wxuser> ulist=wxuserMapper.selectByExample(ue);
		 if(ulist.size()>0){
			 u=ulist.get(0);
		 }else{
			 u=null;
		 }
		return u;
	}
    
    //查询对象
    public Wxuser loadByWxuserId(int id) {
    	Wxuser u=wxuserMapper.selectByPrimaryKey(id);
		return u;
	}
    
    //登录业务
    public Wxuser login(String username,String password){
    	Wxuser u=new Wxuser();
		WxuserExample ue=new WxuserExample();
		Criteria criteria=ue.createCriteria();
		criteria.andUsernameEqualTo(username);
		List<Wxuser> ulist=wxuserMapper.selectByExample(ue);
		 if(ulist.size()>0){
			 u=ulist.get(0);
		 }else{
			 u=null;
		 }
		 if(u==null)throw new RuntimeException("用户名不存在!");		
		 if(!password.equals(u.getPassword()))  throw new RuntimeException("用户密码不正确！");
		 if(u.getStatus()==0) throw new RuntimeException("用户已经停用!");
		return u;
    }

    //查询所有用户
	public List<Wxuser> list() {
		return wxuserMapper.selectByExample(null);
	}
    
}
